<template>
  <a-space direction="vertical">
    <div class="flex items-center justify-between">
      <a-input-search v-if="showKey" v-model:value="searchKey" :placeholder="placeholder" @search="onSearch" @change="onChange" @keydown="onKeyDown" />
      <a-button
        class="ml-1"
        type="primary"
        :disabled="outDoctorStore.currentPatient.isOwnExpense || !outDoctorStore.currentPatient.brId"
        @click="medicalOpen = true"
        >医保</a-button
      >
    </div>
    <a-range-picker
      v-if="showDate"
      v-model:value="searchDate"
      class="w-full"
      format="YYYY-MM-DD"
      value-format="YYYY-MM-DD"
      :allow-clear="false"
      @change="onSearch"
    />
    <!-- 医保登记 -->
    <MedicalRegistration v-model:open="medicalOpen" :patient-info="{ ...outDoctorStore.currentPatient }" @success="onSearch" />
  </a-space>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import dayjs from 'dayjs'
import { debounce } from 'lodash-es'
import useUserStore from '@/store/user'
import useOutDoctorStore from '@/views/out-doctor/store'
import MedicalRegistration from '@/views/out-registration/statistics/register-query/components/MedicalRegistration.vue'

const medicalOpen = ref(false)
const { empId } = useUserStore()
const outDoctorStore = useOutDoctorStore()
const props = defineProps({
  placeholder: {
    type: String,
    default: '请输入'
  },
  value: {
    type: Object,
    default: () => ({})
  },
  showKey: {
    type: Boolean,
    default: true
  },
  showDate: {
    type: Boolean,
    default: true
  }
})
const getParams = () => {
  return {
    ghys: empId,
    ...(props.showKey ? { searchKey: searchKey.value, ghys: empId } : {}),
    ...(props.showDate ? { startDate: searchDate.value?.[0], endDate: searchDate.value?.[1] } : {})
  }
}
const searchKey = ref()
const searchDate = ref([dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')])
const emit = defineEmits(['search', 'update:value'])
const onSearch = () => {
  const params = getParams()
  emit('update:value', params)
  emit('search', params)
}
const onChange = debounce(() => onSearch(), 200)
const onKeyDown = (e) => {
  if (e.code === 'Enter') {
    const params = getParams()
    emit('search', { ...params, enter: true })
  }
}
onMounted(() => {
  emit('update:value', getParams())
  onSearch()
})
//键盘操作
// const { keyboardRowClassName, onKeyDown } = useKeyboard(tableData, onSelect, emit, inputValue, props)
</script>
<style lang="less" scoped></style>
